/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.base;

import me.yokeyword.fragmentation.ISupportAbility;
import me.yokeyword.fragmentation.ISupportFraction;
import me.yokeyword.fragmentation.SupportAbilityDelegate;
import me.yokeyword.fragmentation.SupportHelper;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.multimodalinput.event.TouchEvent;

/**
 * The customized MySupportAbility is displayed. SupportAbility is not inherited.
 */
public class MySupportAbility extends FractionAbility implements ISupportAbility {
    final SupportAbilityDelegate mDelegate = new SupportAbilityDelegate(this);

    @Override
    public SupportAbilityDelegate getSupportDelegate() {
        return mDelegate;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mDelegate.onStart();
    }

    /**
     * Note： return mDelegate.dispatchTouchEvent(ev) || super.dispatchTouchEvent(ev);
     * @param ev Touch Event instance
     * @return true if Touch event processed in subclass
     */
    @Override
    public boolean onTouchEvent(TouchEvent ev) {
        return mDelegate.dispatchTouchEvent(ev) || super.onTouchEvent(ev);
    }

    /**
     * 不建议复写该方法,请使用 {@link #onBackPressedSupport} 代替
     */
    @Override
    final public void onBackPressed() {
        mDelegate.onBackPressed();
    }

    /**
     * 该方法回调时机为,Ability回退栈内Fraction的数量 小于等于1 时,默认finish Ability
     * 请尽量复写该方法,避免复写onBackPress(),以保证SupportFraction内的onBackPressedSupport()回退事件正常执行
     */
    @Override
    public void onBackPressedSupport() {
        mDelegate.onBackPressedSupport();
    }

    /**
     * Causes the Runnable r to be added to the action queue.
     * <p>
     * The runnable will be run after all the previous action has been run.
     * <p>
     * 前面的事务全部执行后 执行该Action
     * @param runnable new Task
     */
    @Override
    public void post(Runnable runnable) {
        mDelegate.post(runnable);
    }

    /****************************************以下为可选方法(Optional methods)******************************************************/
    /**
     * load Root fraction
     * @param containerId resource id
     * @param toFraction fraction
     */
    // 选择性拓展其他方法
    public void loadRootFraction(int containerId, ISupportFraction toFraction) {
        mDelegate.loadRootFraction(containerId, toFraction);
    }

    public void start(ISupportFraction toFraction) {
        mDelegate.start(toFraction);
    }

    /**
     * starting a fraction
     * @param launchMode Same as Ability's LaunchMode.
     * @param toFraction fraction
     */
    public void start(ISupportFraction toFraction, int launchMode) {
        mDelegate.start(toFraction, launchMode);
    }

    /**
     * It is recommended to use {@link SupportFraction#startWithPopTo(ISupportFraction, Class, boolean)}.
     *
     * @see #popTo(Class, boolean)
     * +
     * @see #start(ISupportFraction)
     * @param toFraction fraction
     * @param includeTargetFraction include this fraction while poping
     * @param targetFractionClass target fraction to set
     */
    public void startWithPopTo(ISupportFraction toFraction, Class<?> targetFractionClass, boolean includeTargetFraction) {
        mDelegate.startWithPopTo(toFraction, targetFractionClass, includeTargetFraction);
    }

    /**
     * Pop the fraction.
     */
    public void pop() {
        mDelegate.pop();
    }

    /**
     * 得到位于栈顶Fraction
     * @return get instance of top fraction else null
     */
    public ISupportFraction getTopFraction() {
        return SupportHelper.getTopFraction(mDelegate.getSupportFractionManagerEx());
    }

    /**
     * 获取栈内的fraction对象
     * @param fractionClass fraction class name
     * @return fraction instance if found or null
     */
    public <T extends ISupportFraction> T findFraction(Class<T> fractionClass) {
        return SupportHelper.findFraction(mDelegate.getSupportFractionManagerEx(), fractionClass);
    }
}
